import json

from utils.selenium.perform import WPerform


class ToolType:
    """工具类型"""
    screenshot = 1
    set_cookie = 2
    get_cookie = 3


def run_tool_operation(perform: WPerform, item: dict, other_query: dict):
    tool_type = item.get('tool_type')
    if tool_type == ToolType.screenshot:
        perform.screenshot()
    elif tool_type == ToolType.set_cookie:
        set_cookies = json.loads(item.get('set_cookies'))
        for item in set_cookies:
            perform.driver.add_cookie(item)
    elif tool_type == ToolType.get_cookie:
        get_cookie_key = item.get('get_cookie_key')
        if get_cookie_key:
            perform.send_msg('Cookie Key: [%s]' % get_cookie_key)
            value = perform.driver.get_cookie(get_cookie_key)
            perform.send_json(json.dumps(value))
        else:
            value = perform.driver.get_cookies()
            perform.send_json(json.dumps(value))